home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / bin / ecryptfs-setup-swap < prev    next >
Encoding:
Text File  |  2009-06-04  |  4.5 KB  |  171 lines

  1. #!/bin/sh -e
  2. #    ecryptfs-setup-swap
  3. #    Copyright (C) 2008 Canonical Ltd.
  4. #
  5. #    Authors: Dustin Kirkland <kirkland@canonical.com>
  6. #
  7. #    This program is free software; you can redistribute it and/or modify
  8. #    it under the terms of the GNU General Public License as published by
  9. #    the Free Software Foundation; version 2 of the License.
  10. #
  11. #    This program is distributed in the hope that it will be useful,
  12. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. #    GNU General Public License for more details.
  15. #
  16. #    You should have received a copy of the GNU General Public License
  17. #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  
  19. # The cryptswap setup used here follows a guide published at:
  20. #  * http://ubuntumagnet.com/2007/11/creating-encrypted-swap-file-ubuntu-using-cryptsetup
  21.  
  22. error() {
  23.     echo "ERROR: $1" 1>&2
  24.     exit 1
  25. }
  26.  
  27. info() {
  28.     echo "INFO: $1"
  29. }
  30.  
  31. warning() {
  32.     echo "WARNING: "
  33. }
  34.  
  35. usage() {
  36.     echo
  37.     echo "Usage:"
  38.     echo "  $0 [-f|--force]"
  39.     echo
  40.     exit 1
  41. }
  42.  
  43. # Handle command line options
  44. FORCE=0
  45. while [ ! -z "$1" ]; do
  46.     case "$1" in
  47.         -f|--force)
  48.             FORCE=1
  49.             shift 1
  50.         ;;
  51.         *)
  52.             usage
  53.         ;;
  54.     esac
  55. done
  56.  
  57. # Ensure that cryptsetup is available
  58. [ -x /sbin/cryptsetup ] || error "Please install 'cryptsetup'"
  59.  
  60. # Ensure that we're running with root privileges
  61. [ -w /etc/passwd ] || error "This program must be run with 'sudo', or as root"
  62.  
  63. # Count swap spaces available
  64. # BUG: We only support setting up a single swap space at this time
  65. if [ $(grep -c "^/" /proc/swaps) -eq 0 ]; then
  66.     mem=$(grep "^MemTotal:" /proc/meminfo | awk '{print $2}')
  67.     swapsize=$((4*$mem))
  68.     info "You do not currently have any swap space defined."
  69.     echo
  70.     echo "You can create a swap file by doing:"
  71.     echo " $ sudo dd if=/dev/zero of=/swapfile count=$swapsize"
  72.     echo " $ sudo mkswap /swapfile"
  73.     echo " $ sudo swapon /swapfile"
  74.     echo
  75.     echo "And then re-run $0"
  76.     echo
  77.     exit 0
  78. elif [ $(grep -c "^/" /proc/swaps) -gt 1 ]; then
  79.     info "You have more than one swap space defined."
  80.     error "$0 only supports setting up a single swap space"
  81. else
  82.     swap=$(grep "^/" /proc/swaps | awk '{print $1}')
  83. fi
  84.  
  85. # Make sure this is swap space
  86. if ! vol_id "$swap" | grep -qs "ID_FS_TYPE=swap"; then
  87.     error "[$swap] does not appear to be swap space"
  88. fi
  89.  
  90. # Check if this this swap space is already setup for encryption
  91. if /sbin/dmsetup table "$swap" | grep -qs " crypt " 2>/dev/null; then
  92.     info "[$swap] already appears to be encrypted."
  93.     exit 0
  94. else
  95.     # keep going
  96.     /bin/true
  97. fi
  98.  
  99. base=$(basename "$swap")
  100. if grep -qs "^$base.*swap.*cipher" /etc/crypttab 2>/dev/null; then
  101.     info "[$swap] already has an entry in /etc/crypttab."
  102.     exit 0
  103. fi
  104. if grep -qs "$swap" /etc/initramfs-tools/conf.d/cryptroot 2>/dev/null; then
  105.     info "[$swap] already has an entry in /etc/fstab."
  106.     exit 0
  107. fi
  108.  
  109. # Ensure available dev mapper name 'cryptswap'
  110. if grep -qs "^cryptswap" /etc/crypttab; then
  111.     error "There appears to be a cryptswap entry in /etc/cryptab; aborting."
  112. fi
  113.  
  114. # Ensure available fstab entry
  115. if grep -qs "^/dev/mapper/cryptswap" /etc/fstab; then
  116.     error "There appears to be a cryptswap entry in /etc/fstab; aborting."
  117. fi
  118.  
  119. # Ensure that the existing swap space exists in fstab
  120. if grep -qs "^$swap" /etc/fstab; then
  121.     sed -i "s:^$swap:\#$swap:" /etc/fstab
  122.     info "Commented out your unencrypted swap from /etc/fstab"
  123. else
  124.     info "Your swap space isn't currently listed in /etc/fstab"
  125. fi
  126.  
  127. ##########################################################################
  128. # Warn the user about breaking hibernate mode
  129. if [ "$FORCE" != 1 ]; then
  130.     echo
  131.     warning
  132.     echo "    An encrypted swap is required to help ensure that encrypted files"
  133.     echo "    are not leaked to disk in an unencrypted format."
  134.     echo
  135.     echo "    HOWEVER, THE SWAP ENCRYPTION CONFIGURATION PRODUCED BY THIS PROGRAM"
  136.     echo "    WILL BREAK HIBERNATE/RESUME ON THIS SYSTEM!"
  137.     echo
  138.     echo "    NOTE: Your suspend/resume capabilities will not be affected."
  139.     echo
  140.     echo -n "Do you want to proceed with encrypting your swap [y/N]: "
  141.     CONFIRM=`head -n1`
  142.     echo
  143.     if [ "$CONFIRM" != "y" -a "$CONFIRM" != "Y" ]; then
  144.         echo
  145.         info "Aborting."
  146.         echo
  147.         exit 0
  148.     fi
  149. fi
  150. ##########################################################################
  151.  
  152.  
  153. info "Setting up swap: [$swap]"
  154.  
  155. # Add crypttab entry
  156. echo "cryptswap $swap /dev/urandom swap,cipher=aes-cbc-essiv:sha256" >> /etc/crypttab
  157.  
  158. # Add fstab entry
  159. echo "/dev/mapper/cryptswap none swap sw 0 0" >> /etc/fstab
  160.  
  161. # Turn swap off
  162. swapoff -a
  163.  
  164. # Restart cryptdisks
  165. /etc/init.d/cryptdisks restart
  166.  
  167. # Turn the swap on
  168. swapon -a
  169.  
  170. info "Successfully setup encrypted swap!"
  171.